import logging
import time

from path.path import Path
import os

path = Path()


class Log:
    def __init__(self):
        """日志"""
        self.log = logging.getLogger('log')
        self.log.handlers.clear()
        self.log.setLevel(logging.DEBUG)
        t = time.asctime(time.localtime(time.time())).split(" ")
        file_log = logging.FileHandler(os.path.join(path.root_path, 'log/log.txt'), encoding='utf-8')
        file_log.setLevel(logging.INFO)
        fmt = logging.Formatter(self.fmt)
        file_log.setFormatter(fmt)
        self.log.addHandler(file_log)

    @property
    def fmt(self):
        return '[%(asctime)s]\t[%(name)s]\t[%(levelname)s]\t(%(filename)s:%(funcName)s:%(lineno)d)\t%(message)s'

    def log_action(self, action, test_name):
        """动作日志"""
        self.log.info("没有动作{},跳过测试用例{}".format(action, test_name))

    def log_post(self, url, data):
        self.log.info("POST请求链接：{}".format(url))
        self.log.info("请求参数：{}".format(data))

    def log_get(self, url):
        self.log.info("get请求链接：{}".format(url))

    def log_assert(self, value):
        self.log.info("期望值：{},断言结果：成功".format(value))

    def log_os(self, value, value1):
        self.log.info("成功将{}({})写入缓存".format(value, value1))

    def log_os_x(self, value):
        self.log.info("将{}写入缓存失败".format(value))

    def sql_implement(self, sql):
        self.log.info("成功执行sql:{}".format(sql))

    def sql_assert(self):
        self.log.info("断言sql成功")

    def sql_assert_f(self):
        self.log.info("断言sql失败")

    def log_put(self, value, data):
        self.log.info("PUT请求链接：{}".format(value))
        self.log.info("请求参数{}".format(data))

    def log_delete(self,value):
        self.log.info("delete请求链接：{}".format(value))


